home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / slave1.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  3KB  |  97 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: slave1.c,v 1.2 1997/07/09 13:25:18 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include "pvm3.h"
  35.  
  36. main()
  37. {
  38.     int mytid;       /* my task id */
  39.     int tids[32];    /* task ids   */
  40.     int n, me, i, nproc, master, msgtype;
  41.     float data[100], result;
  42.     float work();
  43.  
  44.     /* enroll in pvm */
  45.     mytid = pvm_mytid();
  46.  
  47.     /* Receive data from master */
  48.     msgtype = 0;
  49.     pvm_recv( -1, msgtype );
  50.     pvm_upkint(&nproc, 1, 1);
  51.     pvm_upkint(tids, nproc, 1);
  52.     pvm_upkint(&n, 1, 1);
  53.     pvm_upkfloat(data, n, 1);
  54.     
  55.     /* Determine which slave I am (0 -- nproc-1) */
  56.     for( i=0; i<nproc ; i++ )
  57.        if( mytid == tids[i] ){ me = i; break; }
  58.  
  59.     /* Do calculations with data */
  60.     result = work( me, n, data, tids, nproc );
  61.  
  62.     /* Send result to master */
  63.     pvm_initsend( PvmDataDefault );
  64.     pvm_pkint( &me, 1, 1 );
  65.     pvm_pkfloat( &result, 1, 1 );
  66.     msgtype = 5;
  67.     master = pvm_parent();
  68.     pvm_send( master, msgtype );
  69.  
  70.     /* Program finished. Exit PVM before stopping */
  71.     pvm_exit();
  72. }
  73.  
  74. float
  75. work(me, n, data, tids, nproc )
  76.     /* Simple example: slaves exchange data with left neighbor (wrapping) */
  77.     int me, n, *tids, nproc;
  78.     float *data;
  79. {
  80.     int i, dest;
  81.     float psum = 0.0;
  82.     float sum = 0.0;
  83.     for( i=0 ; i<n ; i++ ){
  84.        sum += me * data[i];
  85.     }
  86.     /* illustrate node-to-node communication */
  87.     pvm_initsend( PvmDataDefault );
  88.     pvm_pkfloat( &sum, 1, 1 );
  89.     dest = me+1;
  90.     if( dest == nproc ) dest = 0;
  91.     pvm_send( tids[dest], 22 );
  92.     pvm_recv( -1, 22 );
  93.     pvm_upkfloat( &psum, 1, 1 );
  94.  
  95.     return( sum+psum );
  96. }
  97.